﻿using EntityModel;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("yjh/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 当前控制器使用的数据上下文
        /// </summary>
        private readonly HallDbContext _DB;
        public UserController(HallDbContext context)
        {
            _DB = context;
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetAllUser() {
            // ToList : 查询
            //return _DB.EM_User.ToList();
            // _DB : 数据库
            // _DB.EM_User 找到数据库中的EM_User表
            // where select 组装Sql语句
            // 执行sql语句 toList() FirstOrDefault()
            // 顺序 先组装  后执行
            //return _DB.EM_User.Where(w=>w.FirstName=="张").Select(s=>s.FirstName).FirstOrDefault();
            var UserName = _DB.EM_User.Where(w => w.FirstName == "张三").Select(s => new
            {
                s.FirstName,
                s.LastName
            }).FirstOrDefault();

            Result result = new Result();
            result.Data = UserName;
            reault.Code = 200;
            return result;
        }
    }
}
